﻿using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace Assist
{
	public class LineAssistFunc
	{
		public static XYZ GetVector(Line line)
		{
			return line.GetEndPoint(1) - line.GetEndPoint(0);
		}

		public static OverlapType GetOverlapType(UIApplication uiApp, Line line1, Line line2)
		{
			XYZ vector = LineAssistFunc.GetVector(line1);
			XYZ vector2 = LineAssistFunc.GetVector(line2);
			if (!GeoUtil.IsParallel(vector, vector2))
			{
				return OverlapType.eNotOverlap;
			}
			XYZ hyendPoint = line1.GetEndPoint(0);
			XYZ closestPointTo = CurveAssistFunc.GetClosestPointTo(uiApp, line2, hyendPoint, false);
			bool flag = closestPointTo != null && closestPointTo.DistanceTo(line1.GetEndPoint(0)) < LineAssistFunc.precision;
			hyendPoint = line1.GetEndPoint(1);
			closestPointTo = CurveAssistFunc.GetClosestPointTo(uiApp, line2, hyendPoint, false);
			bool flag2 = closestPointTo != null && closestPointTo.DistanceTo(line1.GetEndPoint(1)) < LineAssistFunc.precision;
			hyendPoint = line2.GetEndPoint(0);
			closestPointTo = CurveAssistFunc.GetClosestPointTo(uiApp, line1, hyendPoint, false);
			bool flag3 = closestPointTo != null && closestPointTo.DistanceTo(line2.GetEndPoint(0)) < LineAssistFunc.precision;
			hyendPoint = line2.GetEndPoint(1);
			closestPointTo = CurveAssistFunc.GetClosestPointTo(uiApp, line1, hyendPoint, false);
			bool flag4 = closestPointTo != null && closestPointTo.DistanceTo(line2.GetEndPoint(1)) < LineAssistFunc.precision;
			OverlapType result = OverlapType.eNotOverlap;
			if (flag && flag2 && flag3 && flag4)
			{
				result = OverlapType.eOverlap;
			}
			else if ((flag && flag2 && flag3 && !flag4) || (flag && flag2 && !flag3 && flag4) || (flag && flag2 && !flag3 && !flag4))
			{
				result = OverlapType.e2Cover1;
			}
			else if ((!flag && flag2 && flag3 && flag4) || (flag && !flag2 && flag3 && flag4) || (!flag && !flag2 && flag3 && flag4))
			{
				result = OverlapType.e1Cover2;
			}
			else if ((flag && !flag2 && flag3 && !flag4) || ((flag && !flag2 && !flag3 && flag4) || (!flag && flag2 && flag3 && !flag4)) || (!flag && flag2 && !flag3 && flag4))
			{
				result = OverlapType.ePartOverlap;
			}
			return result;
		}

		public static bool ResetLine(Line lineCurve, XYZ ptStart, XYZ ptEnd)
		{
			XYZ hyendPoint = lineCurve.GetEndPoint(0);
			XYZ hyendPoint2 = lineCurve.GetEndPoint(1);
			double num = lineCurve.GetYJKEndParameter(0);
			double num2 = lineCurve.GetYJKEndParameter(1);
			if (ptStart.DistanceTo(hyendPoint) < LineAssistFunc.precision)
			{
				double num3 = ptEnd.DistanceTo(hyendPoint2);
				num2 -= num3;
			}
			else if (ptEnd.DistanceTo(hyendPoint) < LineAssistFunc.precision)
			{
				double num4 = ptStart.DistanceTo(hyendPoint2);
				num2 -= num4;
			}
			else if (ptStart.DistanceTo(hyendPoint2) < LineAssistFunc.precision)
			{
				double num5 = ptEnd.DistanceTo(hyendPoint);
				num += num5;
			}
			else if (ptEnd.DistanceTo(hyendPoint2) < LineAssistFunc.precision)
			{
				double num6 = ptStart.DistanceTo(hyendPoint);
				num += num6;
			}
			lineCurve.MakeBound(num, num2);
			return true;
		}

		public static double AngleToXAxis(Line line)
		{
			return LineAssistFunc.GetVector(line).AngleOnPlaneTo(XYZ.BasisX, XYZ.BasisZ);
		}

		public static XYZ Midpoint(Line line)
		{
			return new XYZ((line.GetEndPoint(0).X + line.GetEndPoint(1).X) / 2.0, (line.GetEndPoint(0).Y + line.GetEndPoint(1).Y) / 2.0, (line.GetEndPoint(0).Z + line.GetEndPoint(1).Z) / 2.0);
		}

		protected static double precision = 1E-06;
	}
}
